package zhu.common.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class DicDAOImpl extends HibernateDaoSupport implements IDicDAO {

	public Integer getIdByValue(final String dicName, final String valueName,
			final String value) {
		// TODO Auto-generated method stub
		try {
			return (Integer) getHibernateTemplate().execute(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							String hql = "from " + dicName + "  where  "
									+ valueName + " = '" + value + "'";
							Query query = session.createQuery(hql);
							query.setMaxResults(1);
							return query.uniqueResult();
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object getValueById(String dicName,String propertyName,Integer value) {
		// TODO Auto-generated method stub
		try {
			String hsql="from "+ dicName +"  where  "+ propertyName + " = '" + value + "'" ;
			return this.getHibernateTemplate().find(hsql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List getAllValueAndId(String dicName) {
		// TODO Auto-generated method stub
		try {
			String hsql="from "+dicName;
			return this.getHibernateTemplate().find(hsql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}